package com.yunpeng.aidevice.common.controllers.configs;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yunpeng.aidevice.biz.service.IFunctionNodeBizService;
import com.yunpeng.aidevice.common.constant.ConfigConstantValues;
import com.yunpeng.common.dto.Result;
import com.yunpeng.common.util.ParametersCheckUtil;
import com.yunpeng.aidevice.common.vo.FunctionNodeDetailVO;
import com.yunpeng.aidevice.common.vo.FunctionPermissionVO;
import com.yunpeng.aidevice.permission.service.IPermissionOuterService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * FunctionNodeController 功能节点
 *
 * @author : Moore(hemw)
 * @date : 2020-04-15
 */
@RestController
@RequestMapping("/api/functionNode")
public class FunctionNodeController {

    @Resource
    private IFunctionNodeBizService functionNodeBizService;

    @Resource
    private IPermissionOuterService permissionOuterService;

    /**
     * 新增功能节点
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addNew", method = RequestMethod.POST)
    public Result<Void> addNew(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String name = requestParam.getString("name");
        String url = requestParam.getString("url");
        Boolean folder = requestParam.getBoolean("folder");
        String parentCode = requestParam.getString("parentCode");
        ParametersCheckUtil.isNotBlank(name, "功能节点名称为空");
        ParametersCheckUtil.isNotBlank(url, "功能节点路径为空");
        return functionNodeBizService.addNew(name, url, folder, parentCode, ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }

    /**
     * 移除功能节点
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public Result<Void> remove(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String code = requestParam.getString("code");
        ParametersCheckUtil.isNotBlank(code, "功能节点代码为空");
        return functionNodeBizService.remove(code);
    }

    /**
     * 功能节点树
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "treeList", method = RequestMethod.POST)
    public Result<List<FunctionPermissionVO>> treeList() {
        return functionNodeBizService.treeList();
    }

    /**
     * 绑定权限到功能节点
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "bindPermissions", method = RequestMethod.POST)
    public Result<Void> bindPermissions(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String nodeCode = requestParam.getString("nodeCode");
        JSONArray permissionCodesArray = requestParam.getJSONArray("permissionCodes");
        ParametersCheckUtil.isNotBlank(nodeCode, "功能节点代码为空");
        ParametersCheckUtil.collectionIsNotNullOrderEmpty(permissionCodesArray, "权限代码为空");
        List<String> permissionCodes = permissionCodesArray.toJavaList(String.class);
        permissionOuterService.clearWholePermissionCache(ConfigConstantValues.getLoginUserCacheDTO().getCode());
        return functionNodeBizService.bindPermission(nodeCode, permissionCodes, ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }

    /**
     * 功能节点详情视图
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "detail", method = RequestMethod.POST)
    public Result<FunctionNodeDetailVO> detail(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String nodeCode = requestParam.getString("nodeCode");
        ParametersCheckUtil.isNotBlank(nodeCode, "功能节点代码为空");
        return functionNodeBizService.detailInfo(nodeCode);
    }
}
